const TOKEN = 'TOKEN';
// const host = 'http://192.168.2.200/fxsc/';
const host = 'https://www.yuming888.top/fxsc/';
/* network.nonAuthRequest("InitAction/initCategory", {}, function(res) {

}, "", function(res) {

}); */
var functionList = [];//记录方法参数的数组

var doingFlg = false;//正在加载

function nonAuthRequest(url, params, successCallback, msg, failCallback) {
	uni.showLoading({
		title: msg ? msg : '加载中...',
		mask: true,
		success: function(req) {
			uni.request({
				url: host+"action/"+url,
				data: params,
				method: 'POST',
				header: {
					'content-type': 'application/x-www-form-urlencoded',
					'X-Requested-With': 'XMLHttpRequest',
				},
				success: function(res) {
					uni.hideLoading();
					typeof successCallback == "function" && successCallback(res);
				},
				fail: function(res) {
					uni.hideLoading();
					typeof failCallback == "function" && failCallback(res);
				}
			});
		}
	});
}

function request(url, params, successCallback, msg, failCallback) {
	let token = uni.getStorageSync(TOKEN);
	if(token){
		if(doingFlg){
			rememberFun(url, params, successCallback, msg, failCallback);
		}else{
			doingFlg = true;//开始加载
			uni.showLoading({
				title: msg ? msg : '加载中...',
				mask: true,
				success: function(req) {
					params['token'] = uni.getStorageSync('TOKEN');
					//params['deviceId'] = plus.device.imei;
					uni.request({
						url: host+"action/auth/user/"+url,
						data: params,
						method: 'POST',
						header: {
							'content-type': 'application/x-www-form-urlencoded',
							'X-Requested-With': 'XMLHttpRequest'
						},
						success: function(res) {
							uni.hideLoading();
							if (res.statusCode == 403) {
								uni.removeStorageSync(TOKEN);
								uni.showModal({
									title: '提示',
									content: '登录信息失效，请重新登陆！',
									showCancel: false,
									success:function(res){
										if(res.confirm){
											uni.navigateTo({
												url: '/pages/public/login'
											})
										}
									}
								})
								doingFlg = false;
								rememberFun(url, params, successCallback, msg, failCallback);
							}else if(res.statusCode == 9){
								uni.removeStorageSync(TOKEN);
								uni.showModal({
									title: '提示',
									content: '检测到您的账号已在其他设备登录，请退出登录后，重新登陆！',
									showCancel: false,
									success:function(res){
										if(res.confirm){
											uni.navigateTo({
												url: '/pages/public/login'
											})
										}
									}
								})
								doingFlg = false;
								rememberFun(url, params, successCallback, msg, failCallback);
							} else if(res.statusCode == 200){
								typeof successCallback == "function" && successCallback(res);
								doingFlg = false;
								doRememberFun();
							}
						},
						fail: function(res) {
							uni.hideLoading();
							typeof failCallback == "function" && failCallback(res);
							doingFlg = false;
						}
					});
				}
			});
		}
	}else{
		uni.showLoading({
			title: '加载中...',
			mask: true,
			success() {
				setTimeout(function(){
					uni.hideLoading();
					uni.navigateTo({
						url: '/pages/public/login'
					})
				}, 500)
			}
		})
	}
}
/** 记录未访问成功的方法 **/
function rememberFun(url, params, successCallback, msg, failCallback){
	let paramObj  = {};
	
	paramObj.url = url;
	paramObj.params = params;
	paramObj.successCallback = successCallback;
	paramObj.msg = msg;
	paramObj.failCallback = failCallback;
	functionList.push(paramObj);
}
/** 执行记录的方法 **/
function doRememberFun(){
	if (functionList.length > 0){
		let paramObj = functionList[0];
		request(paramObj.url,paramObj.params,paramObj.successCallback,paramObj.msg,paramObj.failCallback);
		if(functionList.length == 1){
			functionList = [];
		}else{
			functionList = functionList.slice(1);
		}
	}
}
function uploadFile(url, filePath, successCallback, msg) {
	uni.showLoading({
		title: msg ? msg : '上传中...',
		mask: true,
		success: function() {
			uni.uploadFile({
				url: url,
				filePath: filePath,
				name: 'file',
				header: {
					'content-type': 'multipart/form-data',
					'X-Requested-With': 'XMLHttpRequest'
				},
				success: function(res) {
					uni.hideLoading();
					typeof successCallback == "function" && successCallback(res);
				}
			})
		}
	});
}

module.exports = {
	nonAuthRequest: nonAuthRequest,
	request: request,
	uploadFile: uploadFile,
	host:host
}
